/*
*
*  ExileServer_MarXet_network_buyNowRequest.sqf
*  Author: WolfkillArcadia
*  © 2016 Arcas Industries
*  This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.
*  To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/.
*/
private["_sessionID","_package","_listingID","_thatOneThingThatISentToTheServer","_vehicleObject","_buyerIsSeller","_playerObject","_stock","_sellersUID","_buyerUID","_price","_playerMoney","_listingArray","_newMoney","_sellerPlayerObject","_sellersMoney","_newSellerMoney","_sellerSessionID","_count","_stats","_buyBackFee","_buyerIsSubscriber","_subscriberBuyBackPrice"];
_sessionID = _this select 0;
_package = _this select 1;
_listingID = _package select 0;
_thatOneThingThatISentToTheServer = _package select 1;
_vehicleObject = "";
_buyerIsSeller = false;
try
{
    _playerObject = _sessionID call ExileServer_system_session_getPlayerObject;

    if (isNull _playerObject) then
	{
		throw "We cant read you as a player on the server-side, please try again later.";
	};
    if (_listingID isEqualTo "") then
    {
        throw "Listing ID mismatched, please try again later.";
    };

    _stock = _listingID call AfterlifeServer_market_confirmStock;

    if (_stock isEqualTo false) then
    {
        throw "This item is no longer available.";
    };

    _sellersUID = _stock select 4;
    _buyerUID = getPlayerUID _playerObject;
    _price = parseNumber(_stock select 3);
	_buyBackPrice = round (_price * 0.2);
	_subscriberBuyBackPrice = round (_price * 0.05);
	_buyerIsSubscriber = isClass(missionConfigFile >> "CfgAfterlifeSubscriber" >> _buyerUID);

    if (_buyerUID isEqualTo _sellersUID) then
    {
        _price = 0 + _buyBackPrice;
        _buyerIsSeller = true;
		if (_buyerIsSubscriber) then
		{
			_price = 0 + _subscriberBuyBackPrice;
		};
    };

    _playerMoney = _playerObject getVariable ["ExileMoney",0];
    if (count(_stock select 2) > 1) then
    {
        _listingArray = _stock select 2;
        if (_buyerIsSeller) then
        {
            _price = _rekeyCost;
        }
        else
        {
            _price = _price + _rekeyCost;
        };
        if (_playerMoney < _price) then
        {
            throw "你没有足够的钱";
        };
    }
    else
    {
        if (_playerMoney < _price) then
        {
            throw "你没有足够的钱";
        };
    };
    _listingID call AfterlifeServer_market_updateStock;
    if (_buyerIsSeller) then
    {
        if (_price > 0) then
        {
            _playerMoney = _playerMoney - _price;
            _playerObject setVariable ["ExileMoney",_playerMoney,true];
            format["setPlayerMoney:%1:%2",_playerMoney,_playerObject getVariable ["ExileDatabaseID", 0]] call ExileServer_system_database_query_fireAndForget;
        };
        [_sessionID,"buyItemBuyerResponse",[_stock,_thatOneThingThatISentToTheServer,_vehicleObject,str(_price)]] call AfterlifeServer_system_network_send_to;
    }
    else
    {
        _newMoney = _playerMoney - _price;
        _playerObject setVariable ["ExileMoney",_newMoney,true];

        format["setPlayerMoney:%1:%2",_newMoney,_playerObject getVariable ["ExileDatabaseID", 0]] call ExileServer_system_database_query_fireAndForget;

        [_sessionID,"buyItemBuyerResponse",[_stock,_thatOneThingThatISentToTheServer,_vehicleObject,str(_price)]] call AfterlifeServer_system_network_send_to;
        _sellerPlayerObject = _sellersUID call AfterlifeServer_util_uidToPlayer;
        if (_sellerPlayerObject isEqualTo "") then
        {
            _stats = format["getPlayerMoney:%1", _sellersUID] call ExileServer_system_database_query_selectSingle;
            _sellersMoney = _stats select 0;
            _newSellerMoney = _sellersMoney + _price;
            format["setPlayerMoney:%1:%2",_newSellerMoney, _sellersUID] call ExileServer_system_database_query_fireAndForget;
        }
        else
        {
            _sellersMoney = _sellerPlayerObject getVariable ["ExileMoney", 0];
            _newSellerMoney = _sellersMoney + _price;
            _sellerSessionID = _sellerPlayerObject getVariable ["ExileSessionID",-1];
            _sellerPlayerObject setVariable ["ExileMoney",_newSellerMoney,true];

            format["setPlayerMoney:%1:%2",_newSellerMoney, _sellersUID] call ExileServer_system_database_query_fireAndForget;

            if !(_sellerSessionID isEqualTo -1) then
            {
                [_sellerSessionID,"buyItemSellerResponse",[_stock]] call AfterlifeServer_system_network_send_to;
            };
        };

        format ["玩家 %1 以$ %4的价格购买了玩家%2的%3",_buyerUID,_sellersUID, (_stock select 2) select 0, _price] call AfterlifeServer_util_log;
    };
}
catch
{
    [_sessionID, "toastRequest", ["ErrorTitleAndText", format ["抱歉!", _exception]]] call ExileServer_system_network_send_to;

    _exception call AfterlifeServer_util_log;

    if (!isNil "_stock" && !(_stock isEqualTo false)) then
    {
        _count = -1;
        {
            if ((_x find _listingID) != -1) then
            {
                _count = _forEachIndex;
            };
        } forEach AfterlifeListing;
        (AfterlifeListing select _count) set [1,1];
    };
};
